package com.wkbb.basic.controller.open;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wkbb.basic.model.OrganizationWebank;
import com.wkbb.basic.model.YoutuFace;
import com.wkbb.basic.service.OrganizationService;
import com.wkbb.basic.service.OrganizationWebankService;
import com.wkbb.basic.service.YouTuService;
import com.wkbb.basic.utils.FileUtil;
import com.wkbb.basic.utils.ImageOfbase64UtilDto;
import com.wkbb.basic.utils.YouTuUtil;
import com.wkbb.common.config.ServiceConfig;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.YoutuFaceDto;
import com.wkbb.common.utils.BeanUtil;
import com.wkbb.common.utils.StringUtil;
import com.wkbb.common.utils.ThreadPoolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dengjingyuan
 * @date 2019-07-29 10:49
 */
@Api(description = "优图人脸识别相关接口")
@RestController
@RequestMapping("/basic/{apiVersion}/open")
@Slf4j
public class YouTuController {

    @Autowired
    private YouTuService youTuService;

    @Autowired
    private ServiceConfig serviceConfig;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private OrganizationWebankService organizationWebankService;

    @ApiOperation(value = "生成优图签名")
    @RequestMapping(value = "/getYouTuSign", method = RequestMethod.GET)
    public ResultDTO<Object> getYouTuSign(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion
    ) {
        try {
            log.info("***********UtuController.getYouTuSign******apiVersion:{}", apiVersion);
            return new ResultDTO<>(youTuService.geYouTuSign());
        } catch (Exception e) {
            log.error("***********UtuController.getYouTuSign******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "个体创建")
    @RequestMapping(value = "/addPerson", method = RequestMethod.POST)
    public ResultDTO<Object> addPerson(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "个体ID", required = true) @RequestParam(name = "personId", required = true) String personId,
            @ApiParam(value = "人脸地址", required = true) @RequestParam(name = "picUrl", required = true) String picUrl,
            @ApiParam(value = "集团ID", required = true) @RequestParam(name = "accountId", required = true) Long accountId,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId
    ) {
        String result = null;
        try {
            if (null == orgId || null == personId || null == picUrl) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }


            log.info("--------开始查询学校微众appid--------orgId:{}",orgId);
            OrganizationWebank organizationWebank =organizationWebankService.getOrganizationWebanksByOrgId(orgId,null);

            if(null==organizationWebank){

                log.info("--------此学校没有绑定微众appid--------orgId:{}",orgId);
                return null;
            }

            log.info("***********UtuController.addJvtPerson******apiVersion:{},orgId:{},personId:{},picUrl:{},accountId:{},userId:{}", apiVersion, orgId, personId, picUrl, accountId, userId);
            result = youTuService.addPerson(orgId, personId, new ImageOfbase64UtilDto(picUrl,FileUtil.encodeImageToBase64(new URL(picUrl))), accountId, userId);
            if (JSON.isValid(result)) {
                return new ResultDTO<>(ErrorCode.ADDORMODIFYFACEERROR);
            }
            if (YouTuUtil.FACESUCCESS.equals(JSONObject.parseObject(result).get(YouTuUtil.ERRORMSG).toString())) {
                return new ResultDTO<>(result);
            } else if (YouTuUtil.ALREADYEXISTS.equals(JSONObject.parseObject(result).get(YouTuUtil.ERRORCODE).toString())) {
                return new ResultDTO<>(ErrorCode.FACEALREADYEXISTS);
            }
        } catch (Exception e) {
            log.error("***********UtuController.addJvtPerson******", e);
            return new ResultDTO<>(false);
        }
        return new ResultDTO<>(result);
    }

    @ApiOperation(value = "个体删除")
    @RequestMapping(value = "/deletePerson", method = RequestMethod.DELETE)
    public ResultDTO<Object> deletePerson(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId
    ) {
        try {
            if (null==orgId||null == userId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.deleteJvtPerson******apiVersion:{},orgId:{},userId:{}", apiVersion, orgId, userId);
            return new ResultDTO<>(youTuService.deletePerson(orgId, userId));
        } catch (Exception e) {
            log.error("***********UtuController.deleteJvtPerson******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "个体删除(用户删除脏数据,person表没对应personId)")
    @RequestMapping(value = "/deletePersonBypersonId", method = RequestMethod.DELETE)
    public ResultDTO<Object> deletePersonBypersonId(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "个体ID串", required = true,defaultValue = "1000001642199753;1000001642199754") @RequestParam(name = "personId", required = true) String personIds,
            @ApiParam(value = "环境名称", required = true,defaultValue = "比如生产:PRO,测试:TEST") @RequestParam(name = "profilesActive", required = true) String profilesActive
    ) {
        try {
            if (null==orgId||null == personIds||null==profilesActive) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.deletePersonBypersonId******apiVersion:{},orgId:{},personIds:{},profilesActive:{}", apiVersion, orgId, personIds,profilesActive);
            return new ResultDTO<>(youTuService.deletePersonBypersonId(orgId, personIds,profilesActive));
        } catch (Exception e) {
            log.error("***********UtuController.deletePersonBypersonId******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "优图和学校personId对比")
    @RequestMapping(value = "/comparePersonId", method = RequestMethod.POST)
    public ResultDTO<Object> comparePersonId(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "环境名称", required = true,defaultValue = "比如生产:PRO,测试:TEST") @RequestParam(name = "profilesActive", required = true) String profilesActive
    ) {
        try {
            if (null==orgId||null==profilesActive) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.comparePersonId******apiVersion:{},orgId:{},userId:{}", apiVersion, orgId, profilesActive);
            return new ResultDTO<>(youTuService.comparePersonId(orgId,profilesActive));
        } catch (Exception e) {
            log.error("***********UtuController.comparePersonId******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "人脸添加")
    @RequestMapping(value = "/addFace", method = RequestMethod.POST)
    public ResultDTO<Object> addFace(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "人脸地址", required = true) @RequestParam(name = "picUrl", required = true) String picUrls,
            @ApiParam(value = "个体ID集合", required = true) @RequestParam(name = "personId", required = true) String personId,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId,
            @ApiParam(value = "1,小程序,后台管理", required = true) @RequestParam(name = "userId", required = true) Integer type,
            @ApiParam(value = "人脸相似度(相似度为整数值:范围80-99)", required = false) @RequestParam(name = "similar", required = false) Short similar
    ) {
        try {
            if (null == picUrls || null == personId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.addFace******apiVersion:{},picUrls:{},personId:{},type:{},similar:{}", apiVersion, picUrls, personId,type,similar);
            return new ResultDTO<>(youTuService.addFace(FileUtil.batchEncodeImageToBase64(picUrls), personId, orgId, userId,type,similar));
        } catch (Exception e) {
            log.error("***********UtuController.addFace******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "人脸删除")
    @RequestMapping(value = "/deleteFace", method = RequestMethod.DELETE)
    public ResultDTO<Object> deleteFace(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "个体ID", required = true) @RequestParam(name = "personId", required = true) String personId,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId
    ) {
        try {
            if ( null == personId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.deleteFace******apiVersion:{},personId:{},orgId:{},userId:{}", apiVersion, personId, orgId);
            return new ResultDTO<>(youTuService.deleteFace( personId, orgId));
        } catch (Exception e) {
            log.error("***********UtuController.deleteFace******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "拉取个体")
    @RequestMapping(value = "/getPersons", method = RequestMethod.GET)
    public ResultDTO<Object> getPersons(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId
    ) {
        try {
            if (null == orgId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.getPersons******apiVersion:{},orgId:{}", apiVersion, orgId);
            return new ResultDTO<>(youTuService.getPersons(orgId));
        } catch (Exception e) {
            log.error("***********UtuController.getPersons******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "拉取人脸")
    @RequestMapping(value = "/getPullFaces", method = RequestMethod.GET)
    public ResultDTO<Object> getPullFaces(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "个体ID", required = true) @RequestParam(name = "personId", required = true) String personId
    ) {
        try {
            if (null == personId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.getPullFaces******apiVersion:{},personId:{}", apiVersion, personId);
            return new ResultDTO<>(youTuService.getPullFaces(personId));
        } catch (Exception e) {
            log.error("***********UtuController.getPullFaces******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "人脸识别")
    @RequestMapping(value = "/getFaceIdentify", method = RequestMethod.GET)
    public ResultDTO<Object> getFaceIdentify(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "人脸地址", required = true) @RequestParam(name = "picUrl", required = true) String picUrl
    ) {
        try {
            if (null == orgId || null == picUrl) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.getFaceIdentify******apiVersion:{},orgId:{},picUrl:{}", apiVersion, orgId, picUrl);
            return new ResultDTO<>(youTuService.getFaceIdentify(orgId, new ImageOfbase64UtilDto(picUrl,FileUtil.encodeImageToBase64(new URL(picUrl)))));
        } catch (Exception e) {
            log.error("***********UtuController.getFaceIdentify******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "人脸检测")
    @RequestMapping(value = "/getFaceDetect", method = RequestMethod.GET)
    public ResultDTO<Object> getFaceDetect(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "人脸地址", required = true) @RequestParam(name = "picUrl", required = true) String picUrl
    ) {
        try {
            if (null == picUrl) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.getfaceDetect******apiVersion:{},picUrl:{}", apiVersion, picUrl);
            return new ResultDTO<>(youTuService.getFaceDetect(new ImageOfbase64UtilDto(picUrl,FileUtil.encodeImageToBase64(new URL(picUrl)))));
        } catch (Exception e) {
            log.error("***********UtuController.getfaceDetect******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "人脸对比")
    @RequestMapping(value = "/getFaceCompare", method = RequestMethod.GET)
    public ResultDTO<Object> getFaceCompare(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "人脸地址A", required = true) @RequestParam(name = "picUrla", required = true) String picUrla,
            @ApiParam(value = "人脸地址B", required = true) @RequestParam(name = "picUrlb", required = true) String picUrlb
    ) {
        try {
            if (null == picUrla || null == picUrlb) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.getfaceDetect******apiVersion:{},picUrlA:{},picUrlB:{}", apiVersion, picUrla, picUrlb);
            return new ResultDTO<>(youTuService.getFaceCompare(new ImageOfbase64UtilDto(picUrla,FileUtil.encodeImageToBase64(new URL(picUrla))), new ImageOfbase64UtilDto(picUrlb,FileUtil.encodeImageToBase64(new URL(picUrlb)))));
        } catch (Exception e) {
            log.error("***********UtuController.getfaceDetect******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "人脸录入入口")
    @RequestMapping(value = "/addUserFace", method = RequestMethod.GET)
    public ResultDTO<Object> addUserFace(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "第一张正脸照片", required = true) @RequestParam(name = "picUrlA", required = true) String picUrlA,
            @ApiParam(value = "第二张正脸照片", required = false) @RequestParam(name = "picUrlB", required = false) String picUrlB,
            @ApiParam(value = "个体ID", required = true) @RequestParam(name = "userCode", required = true) String userCode,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "集团ID", required = true) @RequestParam(name = "accountId", required = true) Long accountId,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId
    ) {
        try {
            if (null == picUrlA || null == userCode) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.getfaceDetect******apiVersion:{},picUrlA:{},picUrlB:{},userCode:{},orgId:{},accountId:{},userId:{}", apiVersion, picUrlA, picUrlB, userCode, orgId, accountId, userId);
            String result = youTuService.addUserFace(new ImageOfbase64UtilDto(picUrlA,FileUtil.encodeImageToBase64(new URL(picUrlA))), userCode,orgId, accountId, userId);
            if (JSON.isValid(result)) {
                if (YouTuUtil.FACENOTCOMPLETE.equals(JSONObject.parseObject(result).get("errorcode"))) {

                    return new ResultDTO<>(ErrorCode.FACENOTCOMPLETE);
                } else if (YouTuUtil.PERSONIDNOTEXIST.equals(JSONObject.parseObject(result).get("errorcode"))) {

                    return new ResultDTO<>(ErrorCode.PERSONIDNOTEXIST);
                } else if (YouTuUtil.ERRORCODETYPE.equals(JSONObject.parseObject(result).get("errorcode"))) {

                    if (null != JSONObject.parseObject(result).get("result") && serviceConfig.getFaceCompare().compareTo(new BigDecimal(JSONObject.parseObject(result).get("result").toString())) == 1) {
                        return new ResultDTO<>(ErrorCode.ACCURACYCOMPARE);
                    }

                    if (null != JSONObject.parseObject(result).get("face_num") && Integer.parseInt(JSONObject.parseObject(result).get("face_num").toString()) == 0) {
                        return new ResultDTO<>(ErrorCode.CHECKFACEERROR);
                    }

                    //如果优图返回成功并且ret_codes数组里面有值,返回对应错误码
                    if (null != JSONObject.parseObject(result).get("ret_codes") && JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).size() > 0) {

                        if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110106)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACE2120007);

                        } else if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110107)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACE2120008);

                        } else if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110108)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACE2120009);

                        } else if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110109)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACE2120010);

                        } else if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110110)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACEERROR);

                        } else if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110111)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACE2120011);

                        } else if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110112)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACE2120012);

                        } else if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110113)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACE2120013);

                        } else if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110114)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACE2120014);

                        } else if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110115)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACE2120015);

                        } else if (JSONObject.parseArray(JSONObject.parseObject(result).get("ret_codes").toString()).get(0).equals(YouTuUtil.ERRORCODETYPE110116)) {
                            return new ResultDTO<>(ErrorCode.CHECKFACE2120016);
                        }
                    }
                } else if (YouTuUtil.ERRORCODETYPE110110.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACEERROR);

                } else if (YouTuUtil.ERRORCODETYPE110114.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.FACETYPEERROR);

                } else if (YouTuUtil.ERRORCODETYPE110106.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACE2120007);

                } else if (YouTuUtil.ERRORCODETYPE110107.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACE2120008);

                } else if (YouTuUtil.ERRORCODETYPE110108.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACE2120009);

                } else if (YouTuUtil.ERRORCODETYPE110109.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACE2120010);

                } else if (YouTuUtil.ERRORCODETYPE110111.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACE2120011);

                } else if (YouTuUtil.ERRORCODETYPE110112.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACE2120012);

                } else if (YouTuUtil.ERRORCODETYPE110113.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACE2120013);

                } else if (YouTuUtil.ERRORCODETYPE110115.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACE2120015);

                } else if (YouTuUtil.ERRORCODETYPE110116.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACE2120016);

                } else if (YouTuUtil.ERRORCODETYPE1305.equals(JSONObject.parseObject(result).get("errorcode"))) {
                    return new ResultDTO<>(ErrorCode.CHECKFACE2120017);
                }

            } else {
                return new ResultDTO<>(ErrorCode.ADDORMODIFYFACEERROR);
            }
            return new ResultDTO<>(result);
        } catch (Exception e) {
            log.error("***********UtuController.getfaceDetect******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "人脸查询")
    @RequestMapping(value = "/selectFaceByuserCode", method = RequestMethod.GET)
    public ResultDTO<List<YoutuFace>> selectFaceByuserCode(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId
    ) {
        try {
            if (null == userId||null == orgId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.getfaceDetect******apiVersion:{},userId:{},orgId:{}", apiVersion, userId,orgId);
            return new ResultDTO<>(youTuService.selectFaceByuserCode(userId,orgId));
        } catch (Exception e) {
            log.error("***********UtuController.getfaceDetect******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "根据userCode人脸查询")
    @RequestMapping(value = "/getFaceDtoByUserCode", method = RequestMethod.GET)
    public ResultDTO<List<YoutuFaceDto>> getFaceDtoByUserCode(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId
    ) {
        try {
            if (null == userId||null == orgId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.getFaceDtoByUserCode******apiVersion:{},userId:{},orgId:{}", apiVersion, userId,orgId);
            List<YoutuFace> youtuFacesList = youTuService.selectFaceByuserCode(userId,orgId);
            List<YoutuFaceDto> list = BeanUtil.copyPropertiesList(youtuFacesList, YoutuFaceDto.class);
            return new ResultDTO<>(list);
        } catch (Exception e) {
            log.error("***********UtuController.getFaceDtoByUserCode******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "人脸录入获取短信验证码")
    @RequestMapping(value = "/getSmsCode", method = RequestMethod.GET)
    public ResultDTO<Object> getSmsCode(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "手机号", required = true) @RequestParam(name = "mobile", required = true) String mobile
    ) {
        try {
            if (null == mobile) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.getSmsCode******apiVersion:{},mobile:{}", apiVersion, mobile);
            return new ResultDTO<>(youTuService.getSmsCode(mobile));
        } catch (Exception e) {
            log.error("***********UtuController.getSmsCode******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "通过orgId删除分组下学校所有人脸")
    @RequestMapping(value = "/deletePersonByOrgId", method = RequestMethod.DELETE)
    public ResultDTO<ArrayList<Object>> deletePersonByOrgId(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId
    ) {
        try {
            if (null == orgId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.deletePersonByOrgId******apiVersion:{},orgId:{}", apiVersion, orgId);
            return new ResultDTO<>(youTuService.deletePersonByOrgId( orgId));
        } catch (Exception e) {
            log.error("***********UtuController.deletePersonByOrgId******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "通过集团accountId删除优图所有人脸(此接口只用户清楚优图数据),接口用完后废弃")
    @RequestMapping(value = "/deletePersonByAccountId", method = RequestMethod.DELETE)
    public ResultDTO<ArrayList<Object>> deletePersonByAccountId(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "集团ID", required = true) @RequestParam(name = "accountId", required = true) Long accountId
    ) {
        try {
            if (null == accountId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.deletePersonByAccountId******apiVersion:{},accountId:{}", apiVersion, accountId);
            return new ResultDTO<>(youTuService.deletePersonByAccountId( accountId));
        } catch (Exception e) {
            log.error("***********UtuController.deletePersonByAccountId******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "微众人脸删除接口(此接口在优图数据修复接口后执行)")
    @RequestMapping(value = "/repairWeBankFace", method = RequestMethod.GET)
    public ResultDTO<Object> repairWeBankFace(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId
    ) {
        try {

            log.info("***********UtuController.repairWeBankFace******");
            youTuService.repairWeBankFace(orgId);
            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********UtuController.repairWeBankFace******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "巨龙盒子删除相应人员(此接口在优图数据修复接口后执行)")
    @RequestMapping(value = "/repairDeleteBoxPerson", method = RequestMethod.GET)
    public ResultDTO<Object> repairDeleteBoxPerson(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion
    ) {
        try {

            log.info("***********UtuController.repairDeleteBoxPerson******");
            youTuService.repairDeleteBoxPerson();
            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********UtuController.repairDeleteBoxPerson******", e);
            return new ResultDTO<>(false);
        }
    }


    @ApiOperation(value = "优图数据修复接口")
    @RequestMapping(value = "/repairYouTuInfo", method = RequestMethod.GET)
    public ResultDTO<Object> repairYouTuInfo(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion
    ) {
        try {

            log.info("***********UtuController.repairYouTuInfo******");
            ThreadPoolUtil.addTask(() -> { youTuService.repairYouTuInfo();});

            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********UtuController.repairYouTuInfo******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "生产优图数据同步后注册修复接口")
    @RequestMapping(value = "/repairProYouTuInfo", method = RequestMethod.GET)
    public ResultDTO<Object> repairProYouTuInfo(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion
    ) {
        try {

            log.info("***********UtuController.repairProYouTuInfo******apiVersion:{},orgId:{}", apiVersion);
            youTuService.repairProYouTuInfo();

            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********UtuController.repairProYouTuInfo******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "老个体删除(用完废弃)")
    @RequestMapping(value = "/deleteOldPerson", method = RequestMethod.DELETE)
    public ResultDTO<Object> deleteOldPerson(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "集团ID", required = true) @RequestParam(name = "accountId", required = true) String accountId
    ) {
        try {
            if (null == accountId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            log.info("***********UtuController.deleteOldPerson******apiVersion:{},orgId:{},personId:{}", apiVersion, accountId);
            youTuService.deleteOldPerson(accountId);
            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********UtuController.deleteOldPerson******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "修复包含外链的人脸")
    @RequestMapping(value = "/repiarFaceLink", method = RequestMethod.POST)
    public ResultDTO<Object> repiarFaceLink(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "外链的前缀", required = true,defaultValue = "s.weikanbaobao.co") @RequestParam(name = "url", required = true) String url
    ) {
        try {
            log.info("***********UtuController.repiarFaceLink******apiVersion:{}", apiVersion);
            if (StringUtil.isEmptyIgnoreBlank(url)) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            youTuService.repiarFaceLink(url);
            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********UtuController.deleteOldPerson******", e);
            return new ResultDTO<>(false);
        }
    }


}